package community.peers.internetradio.storage;

import android.app.Activity;
import android.content.SharedPreferences;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Manage list of last played radios.
 * Store and retrieve from SharedStorage.
 */
public class LastPlayed
{
    // Name of the property to use for storing the list of radios
    protected static final String SETTING_NAME = "last_played";

    // How many items should the list remember max.
    protected static final int MAX_ITEMS = 32;

    public static ArrayList<HashMap<String, String>> retrieveAll(Activity context)
    {
        // Used to serialize Java objects to Json
        Gson gson = new Gson();

        // Get the app preferences
        SharedPreferences appPreferences = context.getPreferences(context.MODE_PRIVATE);

        ArrayList<HashMap<String, String>> lastStations =
                new ArrayList<HashMap<String, String>>();

        // Retrieve list of last played stations
        String lastStationsGson = appPreferences.getString(LastPlayed.SETTING_NAME, null);

        // Convert Json to Java object
        if (lastStationsGson != null)
            lastStations = gson.fromJson(
                    lastStationsGson,
                    new TypeToken<ArrayList<HashMap<String, String>>>(){}.getType());

        return lastStations;
    }

    public static void addNew(Activity context, HashMap<String, String> aRadio)
    {
        // Used to serialize Java objects to Json
        Gson gson = new Gson();

        // Get the app preferences
        SharedPreferences appPreferences = context.getPreferences(context.MODE_PRIVATE);

        // Get stations
        ArrayList<HashMap<String, String>> lastStations = LastPlayed.retrieveAll(context);

        /**
         * Remove all previous instances of this radio.
         * NOTE: Looping the list like this is probably inefficient,
         * but should not be a problem since the list is not supposed to
         * be more than a few 10s of items.
         */
        while (lastStations.remove(aRadio));

        // Add radio at the top of list
        lastStations.add(0, aRadio);

        // Cut off list to prevent it from growing too much
        if (lastStations.size() > LastPlayed.MAX_ITEMS)
            lastStations.subList(LastPlayed.MAX_ITEMS, lastStations.size()).clear();

        // Re-serialize list of radios and save preference
        appPreferences
                .edit()
                .putString(LastPlayed.SETTING_NAME, gson.toJson(lastStations))
                .commit();
    }

    public static void empty (Activity context)
    {
        // Get the app preferences
        SharedPreferences appPreferences = context.getPreferences(context.MODE_PRIVATE);

        // Re-serialize list of radios and save preference
        appPreferences
                .edit()
                .remove(LastPlayed.SETTING_NAME)
                .commit();
    }
}